package com.zt.bus.shiro.controller;

import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.system.model.BusinessException;
import com.zt.user.api.UserApi;
import com.zt.user.user.model.User;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2017/6/14.
 */
@Controller
@RequestMapping(value = "/app")
public class OauthController {

    @RequestMapping(value = "/userInfo")
    @ResponseBody
    public JSONObject getOrderPayment(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        User user = UserApi.getUserByPhone(reqBean.getUserId());
        if (user == null) {
            throw new BusinessException("登录已失效，请重新登录！");
        }
        return Response.response(user.toJSONObject());
    }

    @RequestMapping(value = "/{path}", method = RequestMethod.POST)
    public void app(@PathVariable("path") String path, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        forward(path, request, response);
    }

    @RequestMapping(value = "/{path1}/{path2}", method = RequestMethod.POST)
    public void app(@PathVariable("path1") String path1, @PathVariable("path2") String path2,
            HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward(path1 + "/" + path2, request, response);
    }

    @RequestMapping(value = "/{path1}/{path2}/{path3}", method = RequestMethod.POST)
    public void app(@PathVariable("path1") String path1, @PathVariable("path2") String path2,
            @PathVariable("path3") String path3, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        forward(path1 + "/" + path2 + "/" + path3, request, response);
    }

    private void forward(String path, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd = request.getRequestDispatcher("/" + path + ".do");
        rd.forward(request, response);
    }
}
